// pages/new-pages/my-purse/my-purse.js
const app = getApp();
let store = app.store;
import Toast from '../../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        accountInfo:'0.00'
    },
    getUserAccount() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })
        wx.request({
            url: app.globalData.serverAddress.getUserAccount,
            data: {
                userInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log("getUserAccount response",res.data);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        accountInfo: res.data.BodyContent.Balance
                    });
                } else {
                    Toast(res.data.Message);
                }
            },
            fail: function (res) {
                console.log("getCustomerDetail fail:" + JSON.stringify(res))
            },
            complete: function (res) {
                wx.hideLoading();
            },
        })
    },
    withdrawUser(){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })
        wx.request({
            url: app.globalData.serverAddress.withdrawUser,
            data: {
                UserInfoId: app.userInfo.Id,
                Amount: that.data.accountInfo
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                console.log("withdrawUser response", res.data);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    let account = '1811053977000002';
                    store.dispatch({
                        type: 'CurrentChatTo_Change',
                        payload: `p2p-${account}`
                    })
                    wx.navigateTo({
                        url: `/partials/chating/chating?chatTo=${account}&type=p2p&title=闲小将&amount=` + that.data.accountInfo,
                    });
                } else {
                    Toast(res.data.Message);
                }
            },
            fail: function (res) {
                console.log("getCustomerDetail fail:" + JSON.stringify(res))
            },
            complete: function (res) {
                wx.hideLoading();
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserAccount();
    },
    clickWithdraw(){
        let balance = parseInt(this.data.accountInfo);
        if(balance < 50){
            Toast("单次提现50元起，雇主们在排队等你应聘哟");
            return;
        }
        if (!app.globalData.imLogin){
            Toast('登录状态异常，请重新登录！');
            return;
        }
        this.withdrawUser();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})